package com.jhx.mall.dao.impl;

import com.jhx.mall.dao.BaseDao;
import com.jhx.mall.dao.DtsGoodsSpecificationDao;
import com.jhx.mall.entity.DtsGoodsSpecification;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DtsGoodsSpecificationDaoImpl extends BaseDao implements DtsGoodsSpecificationDao {
    @Override
    public List<DtsGoodsSpecification> selectGoodsSpecificationCountByGoodsId(int goodsId) {
        List<DtsGoodsSpecification> goodsSpecificationList = new ArrayList<>();
        String sql = "SELECT specification,COUNT(specification) as count FROM `dts_goods_specification` where goods_id = ? and deleted = 0 GROUP BY specification";
        rs = super.executeQuery(sql, goodsId);
        try {
            while (rs.next()) {
                DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
                goodsSpecification.setCount(rs.getLong("count"));
                goodsSpecification.setSpecification(rs.getString("specification"));
                goodsSpecificationList.add(goodsSpecification);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return goodsSpecificationList;
    }

    @Override
    public List<DtsGoodsSpecification> selectGoodsSpecificationByGoodsId(int goodsId) {
        List<DtsGoodsSpecification> goodsSpecificationList = new ArrayList<>();
        String sql = "SELECT id,goods_id,`value`,specification,pic_url FROM dts_goods_specification WHERE goods_id = ? and deleted = 0;";
        rs = super.executeQuery(sql, goodsId);
        try {
            while (rs.next()) {
                DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
                goodsSpecification.setId(rs.getLong("id"));
                goodsSpecification.setGoodsId(rs.getLong("goods_id"));
                goodsSpecification.setValue(rs.getString("value"));
                goodsSpecification.setSpecification(rs.getString("specification"));
                goodsSpecification.setPicUrl(rs.getString("pic_url"));
                goodsSpecificationList.add(goodsSpecification);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsSpecificationList;
    }
}
